﻿using EDMXParserV1;
using MVCScaffolder.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace MVCScaffolder
{
    public class ControllersCS
    {
        private SpaceStackStringBuilder sb = new SpaceStackStringBuilder();

        CodeGeneratorProperty CodeGeneratorProperties = null;
        
        public ControllersCS( CodeGeneratorProperty CodeGeneratorProperties )
        {
            this.CodeGeneratorProperties = CodeGeneratorProperties;
            sb = new SpaceStackStringBuilder();

            sb.AppendLine("using System;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Data;");
//            sb.AppendLine("using System.Data.Objects;");
            sb.AppendLine("using System.Data.Entity;");
            sb.AppendLine("using System.Linq;");
            sb.AppendLine("using System.Web;");
            sb.AppendLine("using System.Web.Mvc;");
            sb.AppendLine("using " + CodeGeneratorProperties.NamespaceName + ";");
            sb.AppendLine("using " + CodeGeneratorProperties.NamespaceName + ".Models;");
            sb.AppendLine("");
            sb.AppendLine("namespace " + CodeGeneratorProperties.NamespaceName + ".Controllers");
            sb.AppendLine("{");
            sb.AddSpace();
            if (true)
            {
                sb.AppendLine("public class " + CodeGeneratorProperties.EntityPropertyName + "Controller : Controller");
                sb.AppendLine("{");
                //            sb.AppendLine("        private " + EntityClass.Name + " db = new " + EntityClass.Name + "();");
                String EntityClassName = CodeGeneratorProperties.SelectedEDMX.Runtime.Mappings.Mapping.EntityContainerMapping.CdmEntityContainer;
                sb.AddSpace();
                if (true)
                {
                    sb.AppendLine("private " + EntityClassName + " db = new " + EntityClassName + "();");

                    AddGetIndexMethod();


                    AddIndexPostMethod();

                    foreach (Searchable SearchItem in CodeGeneratorProperties.SearchChoices)
                    {
                        AddClassFilterMethod(SearchItem);
                    }

                    AddConfigureSortingMethod();

                    // Paging Section
                    AddConfigurePagingMethod();
                    AddGetNumberOfPagesToSkipMethod(CodeGeneratorProperties.PropertyClassName);
                    AddGetLastPageMethod(CodeGeneratorProperties.PropertyClassName);
                    //End Paging Section

                    AddGetClassDetailFromEntityMethod();
                    AddGetDateDropDownListOptionsMethod();
                    AddGetNumericalDropDownListOptionsMethod();
                    AddGetBooleanDropDownListOptionsMethod();
                    
                    AddDetailsMethod();

                    AddCreateGetMethod();
                    AddCreatePostMethod();

                    AddEditGetMethod();
                    AddEditPostMethod();

                    AddDeleteGetMethod();
                    AddDeletePostMethod();
                    
                    AddDisposeMethod();
                }
                sb.RemoveSpace();
                sb.AppendLine("}");
            }
            sb.RemoveSpace();
            sb.AppendLine("}");  // end Namespace

        }

        private void AddGetIndexMethod()
        {
            sb.AppendLine();
            sb.AppendLine("//");
            sb.AppendLine("// GET: /" + CodeGeneratorProperties.EntityPropertyName + "/");
            sb.AppendLine();
            sb.AppendLine("public ViewResult Index()");
            sb.AppendLine("{");

            sb.AddSpace();
            if (true)
            {
                GetIndexBaseQuery();

                if (CodeGeneratorProperties.GridChoices.Count > 0)
                {
                    sb.AppendLine();
                    sb.AppendLine("// Get the Query for the Details it doesn't execute it yet");
                    sb.AppendLine("var " + CodeGeneratorProperties.PropertyClassName + "Details = Get" + CodeGeneratorProperties.PropertyClassName + "DetailFrom" + CodeGeneratorProperties.EntityPropertyName + "(" + CodeGeneratorProperties.EntityPropertyName + ");");
                }

                sb.AppendLine();
                sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index " + CodeGeneratorProperties.PropertyClassName + "Index = new " + CodeGeneratorProperties.PropertyClassName + "Index();");

                // Paging Section
                sb.AppendLine();
                sb.AppendLine("// Set the number of Records per page that will be displayed.");
                sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index.RecordsPerPage = 10;");
                sb.AppendLine();
                sb.AppendLine("// Set the page number to the first page when going to the page for the first time.");
                sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index.CurrentPage = 1;");
                sb.AppendLine();
                sb.AppendLine("// Run the Query the maximum number of records available.");
                sb.AppendLine("// This executes a database action");
                sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index.RecordCount = " + CodeGeneratorProperties.EntityPropertyName + ".Count();");

                sb.AppendLine();
                sb.AppendLine("// Configures the database sort order Order by settings of the query being built");
                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = ConfigureSorting(" + CodeGeneratorProperties.PropertyClassName + "Index, " + CodeGeneratorProperties.EntityPropertyName + ");");
                sb.AppendLine();
                sb.AppendLine("// Configures the Paging by settings of the query being built");
                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = ConfigurePaging(" + CodeGeneratorProperties.PropertyClassName + "Index, " + CodeGeneratorProperties.EntityPropertyName + ");");
                // Done paging

                sb.AppendLine();
                sb.AppendLine("// Get the Details from the database with the query + sorting + paging");
                sb.AppendLine("// This exectues a database action");
                sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index." + CodeGeneratorProperties.PropertyClassName + "Details = Get" + CodeGeneratorProperties.PropertyClassName + "DetailFrom" + CodeGeneratorProperties.EntityPropertyName + "(" + CodeGeneratorProperties.EntityPropertyName + ");");

                foreach (Searchable item in CodeGeneratorProperties.SearchChoices)
                {
                    bool UsingDDL = false;
                    if (item.GridChoice.IsNull())
                    {
                        // do nothing
                    }
                    else
                    {
                        sb.AppendLine();
                        sb.AppendLine("// populate the dropdown for the " + item.ColumnName + " column");
                        sb.AppendStart();
                        sb.Append(CodeGeneratorProperties.PropertyClassName + "Index." + item.ColumnName + "SearchOption = new ");
                        sb.Append(GetSelectListFromGridChoice(CodeGeneratorProperties.SelectedEDMX, item.GridChoice, "", CodeGeneratorProperties.SelectedEntitySet));
                        sb.AppendLine();
                        UsingDDL = true;
                    }
                    if (UsingDDL == false)
                    {
                        if (item.Property.Type.Contains("DateTime"))
                        {
                            sb.AppendLine("");
                            sb.AppendLine("// The type of search to perform using date options");
                            sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index." + item.ColumnName + "SearchOption = GetDateDropDownListOptions();");
                        }
                        if (Basics.IsNumericType(item.Property.Type))
                        {
                            sb.AppendLine("");
                            sb.AppendLine("// The type of search to perform using numerical options");
                            sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index." + item.ColumnName + "SearchOption = GetNumericalDropDownListOptions();");
                        }
                        else if (item.Property.Type.Contains("Boolean"))
                        {
                            sb.AppendLine("");
                            sb.AppendLine("// The type of search to perform using boolean dropdown options");
                            sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index." + item.ColumnName + "SearchOption = GetBooleanDropDownListOptions();");
                        }
                    }
                }
                sb.AppendLine();
                sb.AppendLine("return View(" + CodeGeneratorProperties.PropertyClassName + "Index);");
                sb.RemoveSpace();

                sb.AppendLine("}");
            }
        }

        private void AddDeletePostMethod()
        {
            sb.AppendLine();
            sb.AppendLine("//");
            sb.AppendLine("// POST:  /" + CodeGeneratorProperties.EntityPropertyName + "/Delete/5");
            sb.AppendLine();
            sb.AppendLine(@"[HttpPost, ActionName(""Delete"")]");
            sb.AppendLine("public ActionResult DeleteConfirmed(" + CodeGeneratorProperties.PropertyClassPrimaryKeyType + " id)");
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine(CodeGeneratorProperties.PropertyClassName + " " + CodeGeneratorProperties.PropertyClassNameVariable + " = db." + CodeGeneratorProperties.EntityPropertyName + ".Single(o => o." + CodeGeneratorProperties.PropertyClassPrimaryKeyName + " == id);");
                sb.AppendLine("db." + CodeGeneratorProperties.EntityPropertyName + ".Remove(" + CodeGeneratorProperties.PropertyClassNameVariable + ");");
                sb.AppendLine("db.SaveChanges();");
                sb.AppendLine(@"return RedirectToAction(""Index"");");
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
        }

        private void AddDeleteGetMethod()
        {
            sb.AppendLine();
            sb.AppendLine("//");
            sb.AppendLine("// GET:  /" + CodeGeneratorProperties.EntityPropertyName + "/Delete/5");
            sb.AppendLine();
            sb.AppendLine("public ActionResult Delete(" + CodeGeneratorProperties.PropertyClassPrimaryKeyType + " id)");
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine("" + CodeGeneratorProperties.PropertyClassName + " " + CodeGeneratorProperties.PropertyClassNameVariable + " = db." + CodeGeneratorProperties.EntityPropertyName + ".Single(o => o." + CodeGeneratorProperties.PropertyClassPrimaryKeyName + " == id);");
                sb.AppendLine("return View(" + CodeGeneratorProperties.PropertyClassNameVariable + ");");
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
        }

        private void AddEditPostMethod()
        {
            sb.AppendLine();
            sb.AppendLine("//");
            sb.AppendLine("// POST:  /" + CodeGeneratorProperties.EntityPropertyName + "/Edit/5");
            sb.AppendLine();
            sb.AppendLine("[HttpPost]");
            if (CodeGeneratorProperties.UsingDropDownLists)
            {
                sb.AppendLine("public ActionResult Edit(" + CodeGeneratorProperties.PropertyClassName + "CreateOrEdit " + CodeGeneratorProperties.PropertyClassNameVariable + ")");
            }
            else
            {
                sb.AppendLine("public ActionResult Edit(" + CodeGeneratorProperties.PropertyClassName + " " + CodeGeneratorProperties.PropertyClassNameVariable + ")");
            }

            sb.AppendLine("{");

            if (true)
            {
                sb.AddSpace();

                sb.AppendLine("if (ModelState.IsValid)");
                sb.AppendLine("{");
                if (true)
                {
                    sb.AddSpace();


                //        test1child dbtest1child = db.test1child
                //.Where(x => x.test1ChildID == test1child.test1ChildID)
                //.FirstOrDefault();

                //        if (dbtest1child != null)
                //        {
                //            // test1child Currenttest1child = new test1child();
                //            //Currenttest1child.test1ChildID = test1child.test1ChildID;
                //            dbtest1child.Test1ParentID = test1child.DDLTest1ParentID;
                //            dbtest1child.childName = test1child.childName;

                //            db.SaveChanges();
                //        }
                //        return RedirectToAction("Index");

                    sb.AppendLine(CodeGeneratorProperties.PropertyClassName + " db" + CodeGeneratorProperties.PropertyClassName + " = db." + CodeGeneratorProperties.PropertyClassName);
                    sb.AddSpace();
                    if (true)
                    {
                        sb.AppendLine(".Where( x => x." + CodeGeneratorProperties.PropertyClassPrimaryKeyName + " == " + CodeGeneratorProperties.PropertyClassNameVariable + "." + CodeGeneratorProperties.PropertyClassPrimaryKeyName + ")");
                        sb.AppendLine(".FirstOrDefault();");
                        sb.AppendLine();
                    }
                    sb.RemoveSpace();

                    sb.AppendLine("if (db" + CodeGeneratorProperties.PropertyClassName + " != null)");
                    sb.AppendLine("{");
                    sb.AddSpace();
                  //  sb.AppendLine("db" + PropertyClassName + "." + PropertyClassPrimaryKeyName + " = " + PropertyClassName )






                    if (CodeGeneratorProperties.UsingDropDownLists)
                    {
                      //  sb.AppendLine(PropertyClassName + " Current" + PropertyClassName + " = new " + PropertyClassName + "();");



                        foreach (var item in CodeGeneratorProperties.SelectedEntitySet.EntityTypeLink.Properties)
                        {
                            bool GridChoicesFound = false;
                            foreach (GridChoice GridChoice in CodeGeneratorProperties.GridChoices)
                            {
                                if (item.Name == GridChoice.ColumnName)
                                {
                                    GridChoicesFound = true;
                                    sb.AppendLine("db" + CodeGeneratorProperties.PropertyClassName + "." + item.Name + " = " + CodeGeneratorProperties.PropertyClassNameVariable + ".DDL" + item.Name + ";");
                                }
                            }

                            if (GridChoicesFound)
                            {
                                // do nothing
                            }
                            else
                            {
                                if (CodeGeneratorProperties.PropertyClassPrimaryKeyName == item.Name)
                                {
                                    // not setting primary key again.
                                }
                                else
                                {
                                    sb.AppendLine("db" + CodeGeneratorProperties.PropertyClassName + "." + item.Name + " = " + CodeGeneratorProperties.PropertyClassNameVariable + "." + item.Name + ";");
                                }                                
                            }
                        }

                        sb.AppendLine();
                        //sb.AppendLine("db." + EntityPropertyName + ".Attach( Current" + PropertyClassName + " );");
                        //sb.AppendLine();
//                        sb.AppendLine("db.ObjectStateManager.ChangeObjectState( Current" + PropertyClassName + ", EntityState.Modified);");
                    }
                    else
                    {

                        foreach (Property item in CodeGeneratorProperties.SelectedEntitySet.EntityTypeLink.Properties)
                        {
                            if (item.Name == CodeGeneratorProperties.PropertyClassPrimaryKeyName)
                            {
                                // do nothing
                            }
                            else
                            {
                                sb.AppendLine("db" + CodeGeneratorProperties.PropertyClassName + "." + item.Name + " = " + CodeGeneratorProperties.PropertyClassName + "." + item.Name);
                            }
                        }
                    }
                    
                    sb.RemoveSpace();
                    sb.AppendLine("}");

                    sb.AppendLine("db.SaveChanges();");
                    sb.AppendLine();

                    sb.AppendLine(@"return RedirectToAction(""Index"");");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");

                sb.AppendLine("return View(" + CodeGeneratorProperties.PropertyClassNameVariable + ");");
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
        }

        private void AddEditGetMethod()
        {
            sb.AppendLine("");
            sb.AppendLine("//");
            sb.AppendLine("// GET:  /" + CodeGeneratorProperties.EntityPropertyName + "/Edit/5");
            sb.AppendLine();
            sb.AppendLine("public ActionResult Edit(" + CodeGeneratorProperties.PropertyClassPrimaryKeyType + " ID)");
            sb.AppendLine("{");
            if (CodeGeneratorProperties.UsingDropDownLists)
            {
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine(CodeGeneratorProperties.PropertyClassName + " SingleItem = db." + CodeGeneratorProperties.EntityPropertyName + ".Single(o => o." + CodeGeneratorProperties.PropertyClassPrimaryKeyName + " == ID);");
                    sb.AppendLine();
                    sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "CreateOrEdit " + CodeGeneratorProperties.PropertyClassName + "CreateOrEdit = new " + CodeGeneratorProperties.PropertyClassName + "CreateOrEdit();");
                    sb.AppendLine();

                    foreach (var item in CodeGeneratorProperties.SelectedEntitySet.EntityTypeLink.Properties)
                    {
                        bool GridChoicesFound = false;
                        foreach (GridChoice GridChoice in CodeGeneratorProperties.GridChoices)
                        {
                            if (item.Name == GridChoice.ColumnName)
                            {
                                GridChoicesFound = true;

                                if (GridChoice.CanLinkedTableBeNull)
                                {
                                    sb.AppendLine();
                                    sb.AppendLine("//The linked column can be null, if the linked column is null show null, otherwise show the value");

                                    sb.AppendStart();
                                    //Example
                                    //ReportCreateOrEdit.DDLReportGroupID = (SingleItem.ReportGroupID == null ? new Nullable<int>() : SingleItem.ReportGroupID.Value);

                                    //ReportCreateOrEdit.DDLReportGroupID = 
                                    sb.Append(CodeGeneratorProperties.PropertyClassName + @"CreateOrEdit.DDL" + GridChoice.ColumnName + " = ");

                                    //(SingleItem.ReportGroupID == null ? new Nullable<int>()
                                    sb.Append("(SingleItem." + GridChoice.ColumnName + " == null ? new Nullable<" + Basics.GetDataTypeFromTableNameColumnName(GridChoice.PrimaryTable, GridChoice.PrimaryKey, CodeGeneratorProperties.SelectedEntitySet) + ">()");

                                    // : SingleItem.ReportGroupID
                                    sb.Append(" : SingleItem." + GridChoice.ColumnName);
                                }
                                else
                                {

                                    sb.AppendStart();
                                    sb.Append(CodeGeneratorProperties.PropertyClassName + @"CreateOrEdit.DDL" + GridChoice.ColumnName + " = SingleItem." + GridChoice.ColumnName);
                                }

                                //if (item.IsNullable() && item.Type != "String")
                                //{
                                //    sb.Append(".Value");
                                //}
                                if (item.IsNullable())
                                {
                                    if (item.Type == "String")
                                    {
                                        // do nothing
                                    }
                                    else
                                    {
                                        sb.Append(".Value");
                                    }
                                }
                                else
                                {
                                    // do nothing
                                }

                                if (GridChoice.CanLinkedTableBeNull)
                                {
                                    // end of the ? statement for null check
                                    sb.Append(")");
                                }

                                sb.Append(";");
                                sb.AppendLine();

                                sb.AppendLine();
                                sb.AppendLine("// Create a select list with key column " + GridChoice.PrimaryKey + " and display column " + GridChoice.DisplayColumnName);
                                sb.AppendStart();
                                sb.Append(CodeGeneratorProperties.PropertyClassName + @"CreateOrEdit." + GridChoice.ColumnName + " = new ");
                                //new SelectList( db., "id", "description" , SingleItem.applicationId);

                                sb.Append(
                                    GetSelectListFromGridChoice(CodeGeneratorProperties.SelectedEDMX, GridChoice, "SingleItem." + GridChoice.ColumnName, CodeGeneratorProperties.SelectedEntitySet)
                                );

                                //                            sb.Append(item.GetSelectList(Lst, NamespaceName, EntityMappingList, "SingleItem"));
                                sb.AppendLine();
                            }
                        }

                        if (GridChoicesFound)
                        {
                            // do nothing
                        }
                        else
                        {
                            sb.AppendLine(CodeGeneratorProperties.PropertyClassName + @"CreateOrEdit." + item.Name + " = SingleItem." + item.Name + ";");
                        }
                    }

                    sb.AppendLine();
                    sb.AppendLine("return View(" + CodeGeneratorProperties.PropertyClassName + @"CreateOrEdit);");


                }
                sb.RemoveSpace();
            }
            else
            {
                sb.AppendLine(CodeGeneratorProperties.PropertyClassName + " " + CodeGeneratorProperties.PropertyClassNameVariable + " = db." + CodeGeneratorProperties.EntityPropertyName + ".Single(o => o." + CodeGeneratorProperties.PropertyClassPrimaryKeyName + " == ID);");
                sb.AppendLine("return View(" + CodeGeneratorProperties.PropertyClassNameVariable + ");");
            }
            sb.AppendLine("}");
        }

        private void AddConfigureSortingMethod()
        {
            Sortable FirstSortableChoice = null;
            if (true)
            {
                sb.AppendLine();
                sb.AppendLine("private static IQueryable<" + CodeGeneratorProperties.PropertyClassName + "> ConfigureSorting(" + CodeGeneratorProperties.PropertyClassName + "Index " + CodeGeneratorProperties.PropertyClassName + "Index, IQueryable<" + CodeGeneratorProperties.PropertyClassName + "> " + CodeGeneratorProperties.EntityPropertyName + ")");
                sb.AppendLine("{");

                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine("switch (" + CodeGeneratorProperties.PropertyClassName + "Index.SortColumn )");
                    sb.AppendLine("{");
                    if (true)
                    {
                        sb.AddSpace();
                        foreach (Sortable Sortable in CodeGeneratorProperties.SortChoices)
                        {
                            if (FirstSortableChoice.IsNull())
                            {
                                FirstSortableChoice = Sortable;
                            }
                            sb.AppendLine(@"case """ + Sortable.ColumnName + @""":");
                            if (true)
                            {
                                sb.AddSpace();
                                AddSortCode(CodeGeneratorProperties.GridChoices, CodeGeneratorProperties.EntityPropertyName, CodeGeneratorProperties.PropertyClassName, sb, Sortable);
                                sb.AppendLine("break;");
                                sb.RemoveSpace();
                            }

                        }
                        sb.AppendLine("default:");
                        if (true)
                        {
                            sb.AddSpace();
                            AddSortCode(CodeGeneratorProperties.GridChoices, CodeGeneratorProperties.EntityPropertyName, CodeGeneratorProperties.PropertyClassName, sb, FirstSortableChoice);

                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("}");
                    sb.AppendLine("return " + CodeGeneratorProperties.EntityPropertyName + ";");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
            }
        }

        private void AddClassFilterMethod(Searchable SearchItem)
        {
            //                sb.AppendLine(@"            " + EntityPropertyName + " = Add" + SearchItem.ColumnName + "Filter(" + PropertyClassName + "Index, " + EntityPropertyName + ");");

            sb.AppendLine();
            sb.AppendLine(@"//" + SearchItem.ColumnName);
            sb.AppendLine(@"private static IQueryable<" + CodeGeneratorProperties.PropertyClassName + "> Add" + SearchItem.ColumnName + "Filter(" + CodeGeneratorProperties.PropertyClassName + "Index " + CodeGeneratorProperties.PropertyClassName + "Index, IQueryable<" + CodeGeneratorProperties.PropertyClassName + "> " + CodeGeneratorProperties.EntityPropertyName + ")");
            sb.AppendLine(@"{");
            sb.AddSpace();
            //                sb.AppendLine(@"            " + EntityPropertyName + " = Add" + SearchItem.ColumnName + "Filter(" + PropertyClassName + "Index, " + EntityPropertyName + ");");

            //                    Products = AddProductIDFilter(ProductIndex, Products);


            sb.AppendLine();
            sb.AppendLine("if (" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search != null)");
            sb.AppendLine("{");
            sb.AddSpace();
            if (SearchItem.GridChoice.IsNull())
            {
                if (SearchItem.Property.Type.Contains("DateTime"))
                {
                    sb.AppendLine("switch (" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "SearchOptionResult )");
                    sb.AppendLine("{");
                    sb.AddSpace();

                    sb.AppendLine(@"case null:");

                    sb.AddSpace();
                    sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x =>  EntityFunctions.TruncateTime(x." + SearchItem.ColumnName + ") == " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                    sb.AppendLine("break;");
                    sb.RemoveSpace();

                    sb.AppendLine(@"case ""Equals"":");

                    sb.AddSpace();
                    sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x =>  EntityFunctions.TruncateTime(x." + SearchItem.ColumnName + ") == " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                    sb.AppendLine("break;");
                    sb.RemoveSpace();

                    sb.AppendLine(@"case ""Before"":");
                    sb.AddSpace();
                    sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => EntityFunctions.TruncateTime(x." + SearchItem.ColumnName + ") < " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                    sb.AppendLine("break;");
                    sb.RemoveSpace();

                    sb.AppendLine(@"case ""After"":");
                    sb.AddSpace();
                    sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => EntityFunctions.TruncateTime(x." + SearchItem.ColumnName + ") > " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                    sb.AppendLine("break;");
                    sb.RemoveSpace();

                    sb.AppendLine(@"case ""Between"":");
                    sb.AddSpace();
                    sb.AppendLine("if (" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "SearchBetween != null)");
                    sb.AppendLine("{");
                    sb.AddSpace();
                    sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => EntityFunctions.TruncateTime(x." + SearchItem.ColumnName + ") >= " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                    sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => EntityFunctions.TruncateTime(x." + SearchItem.ColumnName + ") <= " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "SearchBetween);");
                    sb.RemoveSpace();
                    sb.AppendLine("}");
                    sb.AppendLine("else");
                    sb.AppendLine("{");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => EntityFunctions.TruncateTime(x." + SearchItem.ColumnName + ") >= " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                        sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " == null);");
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("}");
                    sb.AppendLine("break;");
                    sb.RemoveSpace();

                    sb.AppendLine("default:");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine("break;");
                        sb.RemoveSpace();
                    }
                    sb.RemoveSpace();
                    sb.AppendLine("}");
                }
                else if (Basics.IsNumericType(SearchItem.Property.Type))
                {
                    sb.AppendLine("switch (" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "SearchOptionResult )");
                    sb.AppendLine("{");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine(@"case null:");

                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " == " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }

                        sb.AppendLine(@"case ""="":");

                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " == " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }

                        sb.AppendLine(@"case ""!="":");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " != " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }

                        sb.AppendLine(@"case "">"":");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " > " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }

                        sb.AppendLine(@"case "">="":");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " >= " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine(@"case ""<"":");

                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " < " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine(@"case ""<="":");

                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " <= " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }

                        sb.AppendLine(@"case ""Between Inclusive"":");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("if (" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "SearchBetween != null)");
                            sb.AppendLine("{");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " >= " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " <= " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "SearchBetween);");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine("}");
                            sb.AppendLine("else");
                            sb.AppendLine("{");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " >= " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " == null);");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine("}");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine(@"case ""Not Between Inclusive"":");
                        if (true)
                        {
                            sb.AddSpace();

                            sb.AppendLine("if (" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "SearchBetween != null)");
                            sb.AppendLine("{");

                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => ! (x." + SearchItem.ColumnName + " >= " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search && x." + SearchItem.ColumnName + " <= " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "SearchBetween ) );");
                                sb.RemoveSpace();
                            }

                            sb.AppendLine("}");
                            sb.AppendLine("else");
                            sb.AppendLine("{");

                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " >= " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " == null);");
                                sb.RemoveSpace();
                            }

                            sb.AppendLine("}");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }

                        sb.AppendLine(@"case ""Between Exclusive"":");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("if (" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "SearchBetween != null)");
                            sb.AppendLine("{");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " > " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " < " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "SearchBetween);");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine("}");
                            sb.AppendLine("else");
                            sb.AppendLine("{");
                            if (true)
                            {
                                sb.AddSpace();

                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " > " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " == null);");
                                sb.RemoveSpace();
                            }

                            sb.AppendLine("}");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine(@"case ""Not Between Exclusive"":");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("if (" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "SearchBetween != null)");
                            sb.AppendLine("{");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => !(x." + SearchItem.ColumnName + " > " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search && x." + SearchItem.ColumnName + " < " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "SearchBetween));");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine("}");
                            sb.AppendLine("else");
                            sb.AppendLine("{");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " > " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " == null);");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine("}");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("default:");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("}");
                }
                else if (SearchItem.Property.Type.Contains("Boolean"))
                {
                    sb.AppendLine("switch (" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search )");
                    sb.AppendLine("{");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine(@"case null:");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("// do nothing ");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine(@"case ""Yes"":");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x =>  x." + SearchItem.ColumnName + " == true);");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine(@"case ""No"":");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x =>  x." + SearchItem.ColumnName + " == false);");
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("default:");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("break;");
                            sb.RemoveSpace();
                        }
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("}");

                }
                else if (SearchItem.Property.Type == "Int32")
                {
                    sb.AppendLine("int " + SearchItem.ColumnName + "Search = Convert.ToInt32(" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                    sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " == " + SearchItem.ColumnName + "Search);");
                }
                else if (SearchItem.Property.Type == "Double")
                {
                    sb.AppendLine("Double " + SearchItem.ColumnName + "Search = Convert.ToDouble(" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                    sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " == " + SearchItem.ColumnName + "Search);");
                }
                else if (SearchItem.Property.Type == "Single")
                {
                    sb.AppendLine("Single " + SearchItem.ColumnName + "Search = Convert.ToSingle(" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                    sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " == " + SearchItem.ColumnName + "Search);");
                }
                else if (SearchItem.Property.Type == "Int16")
                {
                    sb.AppendLine("Int16 " + SearchItem.ColumnName + "Search = Convert.ToInt16(" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                    sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " == " + SearchItem.ColumnName + "Search);");
                }
                else if (SearchItem.Property.Type == "short?")
                {
                    sb.AppendLine("short? " + SearchItem.ColumnName + "Search = Convert.ToInt16(" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search);");
                    sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + " == " + SearchItem.ColumnName + "Search);");
                }
                else
                {
                    sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.ColumnName + ".Contains(" + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.ColumnName + "Search));");
                }
            }
            else
            {
                String ConverterTypeName = GetConverterFromTypeName(SearchItem.Property.Type);
                if (ConverterTypeName.Length > 0)
                {
                    sb.AppendLine(SearchItem.Property.Type + " " + SearchItem.GridChoice.ColumnName + "Search = Convert." + GetConverterFromTypeName(SearchItem.Property.Type) + "( " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.GridChoice.ColumnName + "Search );");
                }
                else
                {
                    sb.AppendLine(SearchItem.Property.Type + " " + SearchItem.GridChoice.ColumnName + "Search = " + CodeGeneratorProperties.PropertyClassName + "Index." + SearchItem.GridChoice.ColumnName + "Search;");
                }
                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Where(x => x." + SearchItem.GridChoice.ColumnName + " == " + SearchItem.GridChoice.ColumnName + "Search);");
            }

            sb.RemoveSpace();
            sb.AppendLine("}");
            sb.AppendLine("return " + CodeGeneratorProperties.EntityPropertyName + ";");
            sb.RemoveSpace();
            sb.AppendLine("}");
        }

        private void AddIndexPostMethod()
        {
            sb.AppendLine();
            sb.AppendLine("[HttpPost]");
            sb.AppendLine("public ViewResult Index(" + CodeGeneratorProperties.PropertyClassName + "Index " + CodeGeneratorProperties.PropertyClassName + "Index)");
            sb.AppendLine("{");
            sb.AddSpace();
            GetIndexBaseQuery();
            //sb.AppendLine("            var Groups = db");
            //sb.AppendLine("            .Groups           ");
            //sb.AppendLine("            .Include( x => x.Application );");
            //sb.AppendLine("");

            foreach (Searchable SearchItem in CodeGeneratorProperties.SearchChoices)
            {
                sb.AppendLine();
                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = Add" + SearchItem.ColumnName + "Filter(" + CodeGeneratorProperties.PropertyClassName + "Index, " + CodeGeneratorProperties.EntityPropertyName + ");");
            }

            if (CodeGeneratorProperties.SortChoices.Count() > 0)
            {
                sb.AppendLine();
                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = ConfigureSorting(" + CodeGeneratorProperties.PropertyClassName + "Index, " + CodeGeneratorProperties.EntityPropertyName + ");");
            }

            sb.AppendLine();
            sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = ConfigurePaging(" + CodeGeneratorProperties.PropertyClassName + "Index, " + CodeGeneratorProperties.EntityPropertyName + ");");

            foreach (Searchable item in CodeGeneratorProperties.SearchChoices)
            {
                if (item.GridChoice.IsNull())
                {
                    if (item.Property.Type.Contains("DateTime"))
                    {
                        sb.AppendLine();
                        sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index." + item.ColumnName + "SearchOption = GetDateDropDownListOptions();");
                    }
                    else if (Basics.IsNumericType(item.Property.Type))
                    {
                        sb.AppendLine();
                        sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index." + item.ColumnName + "SearchOption = GetNumericalDropDownListOptions();");
                    }
                    else if (item.Property.Type.Contains("Boolean"))
                    {
                        sb.AppendLine();
                        sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index." + item.ColumnName + "SearchOption = GetBooleanDropDownListOptions();");
                    }
                }
                else
                {
                    //sb.AppendLine("//Searchable + IsNull + T1000");
                    sb.AppendStart();
                    sb.Append(CodeGeneratorProperties.PropertyClassName + "Index." + item.ColumnName + "SearchOption = new ");
                    sb.Append(GetSelectListFromGridChoice(CodeGeneratorProperties.SelectedEDMX, item.GridChoice, CodeGeneratorProperties.PropertyClassName + "Index." + item.ColumnName + "Search", CodeGeneratorProperties.SelectedEntitySet));
                    //sb.Append(item.GridChoice.GetSelectList(Lst, NamespaceName, EntityMappingList));
                    sb.AppendLine();
                }
            }

            sb.AppendLine();
            sb.AppendLine(@"ModelState.Remove(""" + CodeGeneratorProperties.PropertyClassName + @"Details"");");
            //                    sb.AppendLine("            " + PropertyClassName + "Index." + PropertyClassName + "Details =  " + EntityPropertyName + ";");
            //if (GridChoices.Count == 0)
            //{
            //    sb.AppendLine("");
            //    sb.AppendLine("            " + PropertyClassName + "Index." + PropertyClassName + "Details =  " + EntityPropertyName + ";");
            //}
            //else
            //{
            sb.AppendLine();
            sb.AppendLine("// this does a database operation.");
            sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index." + CodeGeneratorProperties.PropertyClassName + "Details =  Get" + CodeGeneratorProperties.PropertyClassName + "DetailFrom" + CodeGeneratorProperties.EntityPropertyName + "(" + CodeGeneratorProperties.EntityPropertyName + ");");
            //}

            sb.AppendLine();
            sb.AppendLine("return View(" + CodeGeneratorProperties.PropertyClassName + "Index);");
            sb.RemoveSpace();
            sb.AppendLine("}");
        }

        private void AddCreatePostMethod()
        {
            sb.AppendLine();
            sb.AppendLine("//");
            sb.AppendLine("// POST:  /" + CodeGeneratorProperties.EntityPropertyName + "/Create");
            sb.AppendLine();
            sb.AppendLine("[HttpPost]");

            if (CodeGeneratorProperties.UsingDropDownLists)
            {
                sb.AppendLine("public ActionResult Create(" + CodeGeneratorProperties.PropertyClassName + "CreateOrEdit " + CodeGeneratorProperties.PropertyClassNameVariable + "CreateOrEdit)");
            }
            else
            {
                sb.AppendLine("public ActionResult Create(" + CodeGeneratorProperties.PropertyClassName + " " + CodeGeneratorProperties.PropertyClassNameVariable + ")");
            }
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine("if (ModelState.IsValid)");
                sb.AppendLine("{");

                if (true)
                {
                    sb.AddSpace();
                    if (CodeGeneratorProperties.UsingDropDownLists)
                    {
                        sb.AppendLine(CodeGeneratorProperties.PropertyClassName + " " + CodeGeneratorProperties.PropertyClassNameVariable + " = new " + CodeGeneratorProperties.PropertyClassName + "();");

                        foreach (var item in CodeGeneratorProperties.SelectedEntitySet.EntityTypeLink.Properties)
                        {
                            bool GridChoicesFound = false;
                            foreach (GridChoice GridChoice in CodeGeneratorProperties.GridChoices)
                            {
                                if (item.Name == GridChoice.ColumnName)
                                {
                                    GridChoicesFound = true;
                                    sb.AppendLine(CodeGeneratorProperties.PropertyClassNameVariable + "." + GridChoice.ColumnName + " = " + CodeGeneratorProperties.PropertyClassNameVariable + "CreateOrEdit.DDL" + GridChoice.ColumnName + ";");
                                }
                            }

                            if (GridChoicesFound)
                            {
                                // do nothing
                            }
                            else
                            {
                                sb.AppendLine(CodeGeneratorProperties.PropertyClassNameVariable + "." + item.Name + " = " + CodeGeneratorProperties.PropertyClassNameVariable + "CreateOrEdit." + item.Name + ";");
                            }
                        }
                    }
                    else
                    {
                        // not UsingLists
                    }
                    sb.AppendLine();
                    sb.AppendLine("db." + CodeGeneratorProperties.EntityPropertyName + ".Add(" + CodeGeneratorProperties.PropertyClassNameVariable + ");");
                    sb.AppendLine();
                    //sb.AppendLine("try");
                    //sb.AppendLine("{");
                    //if (true)
                    //{
                    //    sb.AddSpace();
                        sb.AppendLine("db.SaveChanges();");
                    //    sb.RemoveSpace();
                    //}
                    //sb.AppendLine("}");
                    //sb.AppendLine("catch (System.Data.UpdateException Ex)");
                    //sb.AppendLine("{");
                    //if (true)
                    //{
                    //    sb.AddSpace();
                    //    sb.AppendLine(@"if (Ex.Message.Contains(""DefiningQuery and no <InsertFunction>""))");
                    //    sb.AppendLine("{");
                    //    if (true)
                    //    {
                    //        sb.AddSpace();
                    //        sb.AppendLine("// gota put a primary key on the table in sql server.");
                    //        sb.AppendLine("// Open SQL Server Management studio, find table (tablename) ");
                    //        sb.AppendLine(@"// Right click on (tablename), click ""Design""");
                    //        sb.AppendLine(@"// Right click on column (columnName), click ""Set Primary Key""");
                    //        sb.AppendLine("// Save the changes.");
                    //        sb.AppendLine("// You have to update your model, and build the solution ");
                    //        sb.AppendLine();
                    //        sb.AppendLine(@"throw new Exception(""A Primary key is missing on the table in sql server, then update the model, and build the solution"");");
                    //        sb.RemoveSpace();
                    //    }
                    //    sb.AppendLine("}");
                    //    sb.AppendLine();
                    //    sb.AppendLine(@"throw ( Ex );");

                    //    sb.RemoveSpace();
                    //}
                    //sb.AppendLine("}");
                    sb.AppendLine("");
                    sb.AppendLine(@"return RedirectToAction(""Index"");  ");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine();

                if (CodeGeneratorProperties.UsingDropDownLists)
                {
                    sb.AppendLine("return View(" + CodeGeneratorProperties.PropertyClassNameVariable + "CreateOrEdit);");
                }
                else
                {
                    sb.AppendLine("return View(" + CodeGeneratorProperties.PropertyClassNameVariable + ");");
                }
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
        }

        private void AddCreateGetMethod()
        {
            sb.AppendLine();
            sb.AppendLine("//");
            sb.AppendLine("// GET: /" + CodeGeneratorProperties.EntityPropertyName + "/Create");
            sb.AppendLine();
            sb.AppendLine("public ActionResult Create()");
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();

                if (CodeGeneratorProperties.UsingDropDownLists)
                {
                    //GroupCreateOrEdit GroupEdit = new GroupCreateOrEdit();
                    sb.AppendLine(@"" + CodeGeneratorProperties.PropertyClassName + "CreateOrEdit " + CodeGeneratorProperties.PropertyClassName + "Edit = new " + CodeGeneratorProperties.PropertyClassName + "CreateOrEdit();");

                    foreach (var item in CodeGeneratorProperties.GridChoices)
                    {
                        
                            sb.AppendLine();
                            sb.AppendStart();
                            sb.Append(CodeGeneratorProperties.PropertyClassName + "Edit." + item.ColumnName + " = new ");
                            sb.Append(GetSelectListFromGridChoice(CodeGeneratorProperties.SelectedEDMX, item, "", CodeGeneratorProperties.SelectedEntitySet));
                            sb.AppendLine();
                    }

                    sb.AppendLine();
                    sb.AppendLine(@"return View( " + CodeGeneratorProperties.PropertyClassName + "Edit );");
                }
                else
                {
                    sb.AppendLine("return View();");
                }

                sb.RemoveSpace();
            }
            sb.AppendLine("} ");
        }

        private void AddDetailsMethod()
        {
            sb.AppendLine();
            sb.AppendLine("//");
            sb.AppendLine("// GET: /" + CodeGeneratorProperties.EntityPropertyName + "/Details/5");
            sb.AppendLine();
            sb.AppendLine("public ViewResult Details(" + CodeGeneratorProperties.PropertyClassPrimaryKeyType + " ID)");
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();

                if (CodeGeneratorProperties.UsingDropDownLists)
                {
                    sb.AppendLine("var " + CodeGeneratorProperties.EntityPropertyName + " = db." + CodeGeneratorProperties.EntityPropertyName + "");
                    if (true)
                    {
                        sb.AddSpace();
                        foreach (GridChoice item in CodeGeneratorProperties.GridChoices)
                        {
                            sb.AppendLine(".Include(o => o." + item.PrimaryTable + ")");
                        }

                        sb.AppendLine(".Where(o => o." + CodeGeneratorProperties.SelectedEntitySet.EntityTypeLink.Keys[0] + " == ID);");
                        sb.RemoveSpace();
                    }
                    sb.AppendLine();
                    sb.AppendLine("" + CodeGeneratorProperties.PropertyClassName + "Detail " + CodeGeneratorProperties.PropertyClassName + "Detail = Get" + CodeGeneratorProperties.PropertyClassName + "DetailFrom" + CodeGeneratorProperties.EntityPropertyName + "(" + CodeGeneratorProperties.EntityPropertyName + ").Single();");
                    sb.AppendLine();
                    sb.AppendLine("return View(" + CodeGeneratorProperties.PropertyClassName + "Detail);");
                }
                else
                {
                    sb.AppendLine("" + CodeGeneratorProperties.PropertyClassName + " " + CodeGeneratorProperties.PropertyClassNameVariable + " = db." + CodeGeneratorProperties.EntityPropertyName + ".Single(o => o." + CodeGeneratorProperties.PropertyClassPrimaryKeyName + " == ID);");
                    sb.AppendLine("return View(" + CodeGeneratorProperties.PropertyClassNameVariable + ");");
                }
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
        }

        private void AddDisposeMethod()
        {
            sb.AppendLine();
            sb.AppendLine("protected override void Dispose(bool disposing)");
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine("if(disposing) {");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine("db.Dispose();");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine("base.Dispose(disposing);");
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
        }

        private void AddGetBooleanDropDownListOptionsMethod()
        {
            foreach (Searchable item in CodeGeneratorProperties.SearchChoices)
            {
                if (item.Property.Type.Contains("Boolean"))
                {
                    sb.AppendLine();
                    sb.AppendLine("private IEnumerable<SelectListItem> GetBooleanDropDownListOptions()");
                    sb.AppendLine("{");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine("List<SelectListItem> List = new List<SelectListItem>();");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = """", Value = """", Selected = true } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Yes"", Value = ""Yes"", Selected = false } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""No"", Value = ""No"", Selected = false } );");
                        sb.AppendLine();
                        sb.AppendLine("return List;");
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("}");
                    break;
                }
            }
        }

        private void AddGetNumericalDropDownListOptionsMethod()
        {
            foreach (Searchable item in CodeGeneratorProperties.SearchChoices)
            {
                if (Basics.IsNumericType(item.Property.Type))
                {
                    sb.AppendLine();
                    sb.AppendLine("private IEnumerable<SelectListItem> GetNumericalDropDownListOptions()");
                    sb.AppendLine("{");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine("List<SelectListItem> List = new List<SelectListItem>();");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = """", Value = """", Selected = true } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Equals"", Value = ""="", Selected = false } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Not Equal"", Value = ""!="", Selected = false } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Greater Than"", Value = "">"", Selected = false } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Greater Than Or Equal To"", Value = "">="", Selected = false } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Less Than"", Value = ""<"", Selected = false } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Less Than or Equal To"", Value = ""<="", Selected = false } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Between Inclusive"", Value = ""Between Inclusive"", Selected = false } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Between Exclusive"", Value = ""Between Exclusive"", Selected = false } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Not Between Inclusive"", Value = ""Not Between Inclusive"", Selected = false } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Not Between Exclusive"", Value = ""Not Between Exclusive"", Selected = false } );");
                        sb.AppendLine();
                        sb.AppendLine("return List;");
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("}");
                    break;
                }
            }
        }

        private void AddGetDateDropDownListOptionsMethod()
        {
            // If there is a DateTime field add a loader for the date/time combo box.
            foreach (Searchable item in CodeGeneratorProperties.SearchChoices)
            {
                if (item.Property.Type.Contains("DateTime"))
                {
                    sb.AppendLine();
                    sb.AppendLine("private IEnumerable<SelectListItem> GetDateDropDownListOptions()");
                    sb.AppendLine("{");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine("List<SelectListItem> List = new List<SelectListItem>();");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = """", Value = """", Selected = true } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Equals"", Value = ""Equals"", Selected = false } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Before"", Value = ""Before"", Selected = false } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""After"", Value = ""After"", Selected = false } );");
                        sb.AppendLine(@"List.Add( new SelectListItem { Text = ""Between"", Value = ""Between"", Selected = false } );");
                        sb.AppendLine("");
                        sb.AppendLine("return List;");
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("}");
                    break;
                }
            }
        }

        private void AddGetClassDetailFromEntityMethod()
        {
            sb.AppendLine();
            sb.AppendLine("private IQueryable<" + CodeGeneratorProperties.PropertyClassName + "Detail> Get" + CodeGeneratorProperties.PropertyClassName + "DetailFrom" + CodeGeneratorProperties.EntityPropertyName + "(IQueryable<" + CodeGeneratorProperties.PropertyClassName + "> " + CodeGeneratorProperties.EntityPropertyName + ")");
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine("return " + CodeGeneratorProperties.EntityPropertyName + ".Select(x => new " + CodeGeneratorProperties.PropertyClassName + "Detail");
                sb.AppendLine("{");
                if (true)
                {
                    sb.AddSpace();

                    foreach (GridChoice item in CodeGeneratorProperties.GridChoices)
                    {
                        // todo
                        //              ReportGroupGroupName = (x.ReportGroup == null ? "" : x.ReportGroup.GroupName),
                        //              ConnectionConnectionID = (x.Connection == null ? new Nullable<int>() : x.Connection.ConnectionID),
                        sb.AppendLine(item.PrimaryTable + item.DisplayColumnName + " = x." + item.PrimaryTable + "." + item.DisplayColumnName + ",");
                    }

                    foreach (EDMXParserV1.Property Property in CodeGeneratorProperties.SelectedEntitySet.EntityTypeLink.Properties)
                    {
                        bool GridChoiceFound = false;
                        foreach (GridChoice GridChoice in CodeGeneratorProperties.GridChoices)
                        {
                            if (GridChoice.ColumnName == Property.Name)
                            {
                                GridChoiceFound = true;
                                break;
                            }
                        }

                        if (GridChoiceFound == false)
                        {
                            sb.AppendLine(Property.Name + " = x." + Property.Name + ",");
                        }
                    }
                    sb.RemoveSpace();
                }
                sb.AppendLine("});");
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
        }

        private void AddGetLastPageMethod(String PropertyClassName)
        {
            sb.AppendLine();
            sb.AppendLine("private int GetLastPage(" + CodeGeneratorProperties.PropertyClassName + "Index " + CodeGeneratorProperties.PropertyClassName + "Index)");
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine("int NumberOfPagesToSkip = " + CodeGeneratorProperties.PropertyClassName + "Index.RecordCount / " + CodeGeneratorProperties.PropertyClassName + "Index.RecordsPerPage;");
                sb.AppendLine("");
                sb.AppendLine("int RemainingRecords = " + CodeGeneratorProperties.PropertyClassName + "Index.RecordCount % " + CodeGeneratorProperties.PropertyClassName + "Index.RecordsPerPage;");
                sb.AppendLine("if (RemainingRecords == 0)");
                sb.AppendLine("{");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine("// perfect match.");
                    sb.AppendLine("// do nothing");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine("else");
                sb.AppendLine("{");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine("// there is a remainder so we need to show one more page.");
                    sb.AppendLine("NumberOfPagesToSkip = NumberOfPagesToSkip + 1;");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine();
                sb.AppendLine("return NumberOfPagesToSkip;");
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
        }

        private void AddGetNumberOfPagesToSkipMethod(String PropertyClassName)
        {
            sb.AppendLine();
            sb.AppendLine("private static int GetNumberOfPagesToSkip(" + CodeGeneratorProperties.PropertyClassName + "Index " + CodeGeneratorProperties.PropertyClassName + "Index, int NumberOfPagesToSkip)");
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine("if (" + CodeGeneratorProperties.PropertyClassName + "Index.CurrentPage == 1)");
                sb.AppendLine("{");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine("NumberOfPagesToSkip = 0;");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine("else");
                sb.AppendLine("{");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine("NumberOfPagesToSkip = (" + CodeGeneratorProperties.PropertyClassName + "Index.CurrentPage - 1);");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine("return NumberOfPagesToSkip;");
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
        }

        private void AddConfigurePagingMethod()
        {
            sb.AppendLine();
            sb.AppendLine("private IQueryable<" + CodeGeneratorProperties.PropertyClassName + "> ConfigurePaging(" + CodeGeneratorProperties.PropertyClassName + "Index " + CodeGeneratorProperties.PropertyClassName + "Index, IQueryable<" + CodeGeneratorProperties.PropertyClassName + "> " + CodeGeneratorProperties.EntityPropertyName + ")");
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine("");
                sb.AppendLine("// Paging Section");
                sb.AppendLine("int NumberOfPagesToSkip = 0;");
                sb.AppendLine("");
                sb.AppendLine("int NumberOfRecordsToSkip = NumberOfPagesToSkip * " + CodeGeneratorProperties.PropertyClassName + "Index.RecordsPerPage;");
                sb.AppendLine("");
                sb.AppendLine(@"ModelState.Remove(""RecordCount"");");
                sb.AppendLine();
                sb.AppendLine(@"ModelState.Remove(""CurrentPage"");");
                sb.AppendLine();
                sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index.RecordCount = " + CodeGeneratorProperties.EntityPropertyName + ".Count();");
                sb.AppendLine();
                sb.AppendLine("switch (" + CodeGeneratorProperties.PropertyClassName + "Index.PagingAction)");
                sb.AppendLine("{");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine(@"case ""First"":");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine("NumberOfPagesToSkip = 0;");
                        sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index.CurrentPage = 1;");
                        sb.AppendLine();
                        sb.AppendLine("break;");
                        sb.RemoveSpace();
                    }
                    sb.AppendLine(@"case ""Last"":");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index.CurrentPage = GetLastPage(" + CodeGeneratorProperties.PropertyClassName + "Index);");
                        sb.AppendLine();
                        sb.AppendLine("NumberOfPagesToSkip = GetNumberOfPagesToSkip(" + CodeGeneratorProperties.PropertyClassName + "Index, NumberOfPagesToSkip);");
                        sb.AppendLine("");
                        sb.AppendLine("break;");
                        sb.RemoveSpace();
                    }
                    sb.AppendLine(@"case ""Next"":");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine("// filter options may have changed so need to make sure Next is a viable option.");
                        sb.AppendLine("int PotentialNextPage = " + CodeGeneratorProperties.PropertyClassName + "Index.CurrentPage + 1;");
                        sb.AppendLine("int PotentialNumberOfRecordsToSkip = PotentialNextPage * " + CodeGeneratorProperties.PropertyClassName + "Index.RecordsPerPage;");
                        sb.AppendLine();
                        sb.AppendLine("if (PotentialNumberOfRecordsToSkip >  " + CodeGeneratorProperties.PropertyClassName + "Index.RecordCount)");
                        sb.AppendLine("{");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("// set it to the last page.");
                            sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index.CurrentPage = GetLastPage(" + CodeGeneratorProperties.PropertyClassName + "Index);");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("}");
                        sb.AppendLine("else");
                        sb.AppendLine("{");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index.CurrentPage = PotentialNextPage;");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("}");
                        sb.AppendLine("NumberOfPagesToSkip = GetNumberOfPagesToSkip(" + CodeGeneratorProperties.PropertyClassName + "Index, NumberOfPagesToSkip);");
                        sb.AppendLine("break;");
                        sb.RemoveSpace();
                    }
                    sb.AppendLine(@"case ""Previous"":");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine("// filter options may have changed so need to make sure Next is a viable option.");
                        sb.AppendLine("int PreviousPotentialNextPage = " + CodeGeneratorProperties.PropertyClassName + "Index.CurrentPage - 1;");
                        sb.AppendLine("if (PreviousPotentialNextPage < 1)");
                        sb.AppendLine("{");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index.CurrentPage = 1;");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("}");
                        sb.AppendLine("else");
                        sb.AppendLine("{");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("int PreviousPotentialNumberOfRecordsToSkip = PreviousPotentialNextPage * " + CodeGeneratorProperties.PropertyClassName + "Index.RecordsPerPage;");
                            sb.AppendLine("if (PreviousPotentialNumberOfRecordsToSkip > " + CodeGeneratorProperties.PropertyClassName + "Index.RecordCount)");
                            sb.AppendLine("{");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine("// set it to the last page.");
                                sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index.CurrentPage = GetLastPage(" + CodeGeneratorProperties.PropertyClassName + "Index);");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine("}");
                            sb.AppendLine("else");
                            sb.AppendLine("{");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(CodeGeneratorProperties.PropertyClassName + "Index.CurrentPage = PreviousPotentialNextPage;");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine("}");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("}");
                        sb.AppendLine("NumberOfPagesToSkip = GetNumberOfPagesToSkip(" + CodeGeneratorProperties.PropertyClassName + "Index, NumberOfPagesToSkip);");
                        sb.AppendLine("break;");
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("default:");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine("break;");
                        sb.RemoveSpace();
                    }
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine("");
                sb.AppendLine("NumberOfRecordsToSkip = NumberOfPagesToSkip * " + CodeGeneratorProperties.PropertyClassName + "Index.RecordsPerPage;");
                sb.AppendLine("");
                sb.AppendLine(CodeGeneratorProperties.EntityPropertyName + " = " + CodeGeneratorProperties.EntityPropertyName + ".Skip(NumberOfRecordsToSkip)");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine(".Take(" + CodeGeneratorProperties.PropertyClassName + "Index.RecordsPerPage);");
                    sb.RemoveSpace();
                }
                sb.AppendLine("return " + CodeGeneratorProperties.EntityPropertyName + ";");
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
        }

        private static void AddSortCode(List<GridChoice> GridChoices, String EntityPropertyName, String PropertyClassName, SpaceStackStringBuilder sb, Sortable Sortable)
        {
            sb.AppendLine(@"if (" + PropertyClassName + @"Index.SortDirection == ""desc"")");
            sb.AppendLine("{");


            GridChoice CurrentGridChoice = null;
            foreach (GridChoice GridChoice in GridChoices)
            {
                if (GridChoice.ColumnName == Sortable.ColumnName)
                {
                    CurrentGridChoice = GridChoice;
                }
            }

            if (CurrentGridChoice.IsNull())
            {
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine(EntityPropertyName + " = " + EntityPropertyName + ".OrderByDescending(x => x." + Sortable.ColumnName + ");");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine("else");
                sb.AppendLine("{");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine(EntityPropertyName + " = " + EntityPropertyName + ".OrderBy(x => x." + Sortable.ColumnName + ");");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
            }
            else
            {
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine(EntityPropertyName + " = " + EntityPropertyName + ".OrderByDescending(x => x." + CurrentGridChoice.PrimaryTable + "." + CurrentGridChoice.DisplayColumnName + ");");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine("else");
                sb.AppendLine("{");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine("" + EntityPropertyName + " = " + EntityPropertyName + ".OrderBy(x => x." + CurrentGridChoice.PrimaryTable + "." + CurrentGridChoice.DisplayColumnName + ");");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
            }
        }

        private String GetConverterFromTypeName(String TypeName)
        {
            switch (TypeName)
            {
                case "Int32":
                    return "ToInt32";
                default:
                    Debug.WriteLine("GetConverterFromTypeName failure: " + TypeName);
                    break;
            }
            return "";
        }

        private void GetIndexBaseQuery()
        {
            sb.AppendLine("IQueryable<" + CodeGeneratorProperties.PropertyClassName + "> " + CodeGeneratorProperties.EntityPropertyName + " = db");
            sb.AddSpace();
            sb.AppendStart();
            sb.Append("." + CodeGeneratorProperties.EntityPropertyName + "");
            sb.RemoveSpace();

            foreach (GridChoice item in CodeGeneratorProperties.GridChoices)
            {
                sb.AppendLine();
                sb.AddSpace();
                sb.AppendStart();
                sb.Append(".Include(x => x." + item.PrimaryTable + ")");
                sb.RemoveSpace();
            }
            sb.Append(";");
            sb.AppendLine();
        }

        private String GetSelectListFromGridChoice(EDMXParserV1.Edmx Edmx, GridChoice GridChoice, String Default, EntitySet SelectedEntitySet)
        {
            StringBuilder sb = new StringBuilder();

            //foreach (EntitySet EntitySet in Edmx.Runtime.ConceptualModels.Schema.EntityContainer.EntitySets)
            //{
            //    if (EntitySet.EntityTypeLink.Name == GridChoice.PrimaryTable)
            //    {
            //        sb.Append("SelectList( db." + EntitySet.Name + @", """ + GridChoice.PrimaryKey + @""", """ + GridChoice.DisplayColumnName + @"""");
            //        if (Default.Length > 0)
            //        {
            //            sb.Append(" ," + Default + " ");
            //        }
            //        else
            //        {
            //            // do nothing
            //        }

            //        sb.Append(");");
            //        break;
            //    }
            //}
            foreach (NavigationProperty Item in SelectedEntitySet.EntityTypeLink.NavigationProperties)
            {
                if (GridChoice.PrimaryTable == Item.Name)
                {
                    if (Item.ToEntityType != null)
                    {
                        foreach (EDMXParserV1.Property Property in Item.ToEntityType.Properties)
                        {
                            if (Property.Name == GridChoice.DisplayColumnName)
                            {
                                foreach (EntitySet EntitySet in Edmx.Runtime.ConceptualModels.Schema.EntityContainer.EntitySets)
                                {
                                    if (EntitySet.EntityTypeLink.Name == Item.ToRole)
                                    {
                                        sb.Append("SelectList( db." + EntitySet.Name + @", """ + GridChoice.PrimaryKey + @""", """ + GridChoice.DisplayColumnName + @"""");
                                        if (Default.Length > 0)
                                        {
                                            sb.Append(" ," + Default + " ");
                                        }
                                        else
                                        {
                                            // do nothing
                                        }

                                        sb.Append(");");
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return sb.ToString();
        }

        public override string ToString()
        {
            return sb.ToString();
        }
    }
}
